/**
 * 作者: hxl
 * 日期: 2025-03-11 09:40:36
 * 备注: 用户限时冲榜信息表
 */
package com.motu.monstercity.server.game.userdata;

import com.motu.monstercity.protocol.Common.PBUserRankAct;
import com.motu.vertx.module.utility.model.BaseModel;
import com.motu.vertx.module.utility.model.Model;
import io.vertx.core.json.JsonObject;
import io.vertx.core.json.JsonArray;

public class UserRankActModel extends BaseModel implements Model {
    public static final String LISTKEY = "user_id.act_id";
    public static final int REDIS_EXPIRE_TIME = 0;// redis过期时间
    public static final int UPDATE_DB_TYPE = UPDATE_DB_TYPE_QUEUE;// 数据入库类型 队列入库
    public static final boolean ORDER_LIST = true;// 是否需要额外的有序列表

    protected long id; // id
    protected long userId; // 玩家id
    protected long actId; // 活动id
    protected long round; // 活动期数
    protected String rankGet = ""; // 排行榜奖励领取
    protected String taskGet = ""; // 任务奖励领取
    protected long reissueRound; // 补发轮次
    protected String rechargeTaskGet = ""; // 充值任务领取
    protected String rechargeBuy = ""; // 每日特惠礼包购买
    protected long resetTime; // 每日重置时间
    protected String rechargeDayBuy = ""; // 每日购买

    public void reset() {
    }

    protected String getKey() {
        return "UserRankAct." + userId + "." + actId;
    }

    protected String getListKey() {
        return "UserRankAct." + userId;
    }

    protected String getListItemKey() {
        return actId + "";
    }

    public void doCreate() {
        setTableId();
        insert();
    }

    public void setTableId() {
        putId(incrTableId("user_rank_act"));
    }

    public JsonObject insert() {
        return super.insert(getKey(), id, REDIS_EXPIRE_TIME, UPDATE_DB_TYPE, LISTKEY, getListKey(), getListItemKey(), ORDER_LIST);
    }

    public void delete() {
        super.delete(getKey(), id, UPDATE_DB_TYPE, userId, LISTKEY, getListKey(), getListItemKey(), ORDER_LIST);
    }

    /**
     * 根据入库类型更新
     */
    public JsonObject update() {
        return super.update(getKey(), id, REDIS_EXPIRE_TIME, UPDATE_DB_TYPE, userId, getAllObj(), LISTKEY, getListKey(), getListItemKey());
    }

    /**
     * 实时入队列
     */
    public JsonObject updateQueueNow() {
        return super.update(getKey(), id, REDIS_EXPIRE_TIME, UPDATE_DB_TYPE, 0, getAllObj(), LISTKEY, getListKey(), getListItemKey());
    }

    public JsonObject getAllObj(){
        JsonObject obj = new JsonObject();
        obj.put("id", id); // id
        obj.put("userId", userId); // 玩家id
        obj.put("actId", actId); // 活动id
        obj.put("round", round); // 活动期数
        obj.put("rankGet", rankGet); // 排行榜奖励领取
        obj.put("taskGet", taskGet); // 任务奖励领取
        obj.put("reissueRound", reissueRound); // 补发轮次
        obj.put("rechargeTaskGet", rechargeTaskGet); // 充值任务领取
        obj.put("rechargeBuy", rechargeBuy); // 每日特惠礼包购买
        obj.put("resetTime", resetTime); // 每日重置时间
        obj.put("rechargeDayBuy", rechargeDayBuy); // 每日购买
        return obj;
    }

    public PBUserRankAct.Builder takeInitBuilder() {
        PBUserRankAct.Builder sb = PBUserRankAct.newBuilder();
        if (round != 0) {
            sb.setRound(round);
        }
        if (this.rankGet != null && this.rankGet.length() > 0) {
            JsonArray list = new JsonArray(this.rankGet);
            for(int i=0; i<list.size(); i++) {
                sb.addRankGet(list.getInteger(i));
            }
        }
        if (this.taskGet != null && this.taskGet.length() > 0) {
            JsonArray list = new JsonArray(this.taskGet);
            for(int i=0; i<list.size(); i++) {
                sb.addTaskGet(list.getInteger(i));
            }
        }
        if (this.rechargeTaskGet != null && this.rechargeTaskGet.length() > 0) {
            JsonArray list = new JsonArray(this.rechargeTaskGet);
            for(int i=0; i<list.size(); i++) {
                sb.addRechargeTaskGet(list.getInteger(i));
            }
        }
        if (this.rechargeBuy != null && this.rechargeBuy.length() > 0) {
            sb.setRechargeBuy(rechargeBuy);
        }
        if (this.rechargeDayBuy != null && this.rechargeDayBuy.length() > 0) {
            sb.setRechargeDayBuy(rechargeDayBuy);
        }
        return sb;
    }

    public PBUserRankAct.Builder takeBuilder() {
        if (updateObj == null || updateObj.isEmpty()) {
            return takeInitBuilder();
        }
        PBUserRankAct.Builder sb = PBUserRankAct.newBuilder();
        if (isFieldUpdate("round")) {
            sb.setRound(round);
        }
        if (isFieldUpdate("rankGet")) {
            if (this.rankGet != null && this.rankGet.length() > 0) {
                JsonArray list = new JsonArray(this.rankGet);
                for(int i=0; i<list.size(); i++) {
                    sb.addRankGet(list.getInteger(i));
                }
                if (list.isEmpty()) {
                    sb.clearRankGet();
                }
            } else {
                sb.clearRankGet();
            }
        }
        if (isFieldUpdate("taskGet")) {
            if (this.taskGet != null && this.taskGet.length() > 0) {
                JsonArray list = new JsonArray(this.taskGet);
                for(int i=0; i<list.size(); i++) {
                    sb.addTaskGet(list.getInteger(i));
                }
                if (list.isEmpty()) {
                    sb.clearTaskGet();
                }
            } else {
                sb.clearTaskGet();
            }
        }
        if (isFieldUpdate("rechargeTaskGet")) {
            if (this.rechargeTaskGet != null && this.rechargeTaskGet.length() > 0) {
                JsonArray list = new JsonArray(this.rechargeTaskGet);
                for(int i=0; i<list.size(); i++) {
                    sb.addRechargeTaskGet(list.getInteger(i));
                }
                if (list.isEmpty()) {
                    sb.clearRechargeTaskGet();
                }
            } else {
                sb.clearRechargeTaskGet();
            }
        }
        if (isFieldUpdate("rechargeBuy")) {
            sb.setRechargeBuy(this.rechargeBuy);
        }
        if (isFieldUpdate("rechargeDayBuy")) {
            sb.setRechargeDayBuy(this.rechargeDayBuy);
        }
        return sb;
    }

    public long getId() {
        return id;
    }

    public void putId(long id) {
        if (this.id != id) {
            this.id = id;
            updateRedisHashField(getKey(), "id", this.id);
        }
    }

    public void addId(long value) {
        this.id += value;
        updateRedisHashField(getKey(), "id", this.id);
    }

    public long getUserId() {
        return userId;
    }

    public void putUserId(long userId) {
        if (this.userId != userId) {
            this.userId = userId;
            updateRedisHashField(getKey(), "userId", this.userId);
        }
    }

    public void addUserId(long value) {
        this.userId += value;
        updateRedisHashField(getKey(), "userId", this.userId);
    }

    public long getActId() {
        return actId;
    }

    public void putActId(long actId) {
        if (this.actId != actId) {
            this.actId = actId;
            updateRedisHashField(getKey(), "actId", this.actId);
        }
    }

    public void addActId(long value) {
        this.actId += value;
        updateRedisHashField(getKey(), "actId", this.actId);
    }

    public long getRound() {
        return round;
    }

    public void putRound(long round) {
        if (this.round != round) {
            this.round = round;
            updateRedisHashField(getKey(), "round", this.round);
        }
    }

    public void addRound(long value) {
        this.round += value;
        updateRedisHashField(getKey(), "round", this.round);
    }

    public JsonArray getRankGetArray() {
        if (this.rankGet != null && this.rankGet.length() > 0) {
            return new JsonArray(this.rankGet);
        } else {
            return new JsonArray();
        }
    }

    public String getRankGet() {
        return rankGet;
    }

    public void putRankGet(String rankGet) {
        if (!this.rankGet.equals(rankGet)) {
            this.rankGet = rankGet;
            updateRedisHashField(getKey(), "rankGet", this.rankGet);
        }
    }

    public JsonArray getTaskGetArray() {
        if (this.taskGet != null && this.taskGet.length() > 0) {
            return new JsonArray(this.taskGet);
        } else {
            return new JsonArray();
        }
    }

    public String getTaskGet() {
        return taskGet;
    }

    public void putTaskGet(String taskGet) {
        if (!this.taskGet.equals(taskGet)) {
            this.taskGet = taskGet;
            updateRedisHashField(getKey(), "taskGet", this.taskGet);
        }
    }

    public long getReissueRound() {
        return reissueRound;
    }

    public void putReissueRound(long reissueRound) {
        if (this.reissueRound != reissueRound) {
            this.reissueRound = reissueRound;
            updateRedisHashField(getKey(), "reissueRound", this.reissueRound);
        }
    }

    public void addReissueRound(long value) {
        this.reissueRound += value;
        updateRedisHashField(getKey(), "reissueRound", this.reissueRound);
    }

    public JsonArray getRechargeTaskGetArray() {
        if (this.rechargeTaskGet != null && this.rechargeTaskGet.length() > 0) {
            return new JsonArray(this.rechargeTaskGet);
        } else {
            return new JsonArray();
        }
    }

    public String getRechargeTaskGet() {
        return rechargeTaskGet;
    }

    public void putRechargeTaskGet(String rechargeTaskGet) {
        if (!this.rechargeTaskGet.equals(rechargeTaskGet)) {
            this.rechargeTaskGet = rechargeTaskGet;
            updateRedisHashField(getKey(), "rechargeTaskGet", this.rechargeTaskGet);
        }
    }

    public String getRechargeBuy() {
        return rechargeBuy;
    }

    public void putRechargeBuy(String rechargeBuy) {
        if (!this.rechargeBuy.equals(rechargeBuy)) {
            this.rechargeBuy = rechargeBuy;
            updateRedisHashField(getKey(), "rechargeBuy", this.rechargeBuy);
        }
    }

    public long getResetTime() {
        return resetTime;
    }

    public void putResetTime(long resetTime) {
        if (this.resetTime != resetTime) {
            this.resetTime = resetTime;
            updateRedisHashField(getKey(), "resetTime", this.resetTime);
        }
    }

    public void addResetTime(long value) {
        this.resetTime += value;
        updateRedisHashField(getKey(), "resetTime", this.resetTime);
    }

    public String getRechargeDayBuy() {
        return rechargeDayBuy;
    }

    public void putRechargeDayBuy(String rechargeDayBuy) {
        if (!this.rechargeDayBuy.equals(rechargeDayBuy)) {
            this.rechargeDayBuy = rechargeDayBuy;
            updateRedisHashField(getKey(), "rechargeDayBuy", this.rechargeDayBuy);
        }
    }

}